#include<bits/stdc++.h>
using namespace std;
const int N=50005;

int n,m;
int fath[3*N];

int find(int x){
    if(x==fath[x]) return x;
    return fath[x] = find(fath[x]);
}

int ans = 0;
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=3*n;i++) fath[i] = i;
    int op,x,y;
    for(int i=1;i<=m;i++){
        scanf("%d%d%d",&op,&x,&y);
        if(x>n||y>n){
            ans++;
            continue;
        }
        int rx = find(x);
        int ry = find(y);
        if(op==1){
            if(rx==find(y+n)||rx==find(y+2*n)
            ||ry==find(x+n)||ry==find(x+2*n)){
                ans++;
            }
            else{
                fath[rx] = ry;
                fath[find(x+n)] = find(y+n);
                fath[find(x+2*n)] = find(y+2*n);
            }
        }
        else if(op==2){
            if(rx==ry||rx==find(y+n)||ry==find(x+2*n)){
                ans++;
            }
            else{
                fath[rx] = find(y+2*n);
                fath[ry] = find(x+n);
                fath[find(x+2*n)] = find(y+n);
            }
        }
    }
    printf("%d\n",ans);
    return 0;
}